const crypto = require('crypto');

// key和iv密钥参数   data明文
const encrypt = function (key, iv, data) {
    // 定义解密算法
    var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
    // 对明文数据进行字节编码
    var crypted = cipher.update(data, 'utf8', 'binary');
    crypted += cipher.final('binary');
    // 对密文数据进行base64编码
    crypted = Buffer.from(crypted, 'binary').toString('base64');
    return crypted;
};

let key = '751f621ea5c8f930';
let iv = '2624750004598718';
let data = "Hello, nodejs. 演示aes-128-cbc加密和解密";
let crypted = encrypt(key, iv, data);
console.log("数据加密后:", crypted);